package com.dongdong.controller;

import com.dongdong.em.Result;
import com.dongdong.page.PageParams;
import com.dongdong.service.TypeTemplateService;
import com.dongdong.vo.SpecVO;
import com.dongdong.vo.TypeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("type")
@RefreshScope      //热更新
public class TypeTemplateController {

    @Autowired
    private TypeTemplateService typeTemplateService;

    @PostMapping("getTypeList")
    public Result getTypeList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        PageParams<TypeVO> typelist = typeTemplateService.getTypeList(pageNum, pageSize);
        return Result.OK().setData(typelist);
    }

    //根据id查询模板信息
    @GetMapping("getTypeById")
    public Result getTypeById(@RequestParam Long id) {
        TypeVO voList = typeTemplateService.getTypeById(id);
        return Result.OK().setData(voList);
    }

    //修改
    @PutMapping("updateType")
    public Result updateType(@RequestBody TypeVO typeVO) {
        int i = typeTemplateService.updateType(typeVO);
        if (i <= 0) {
            return Result.ERROR();
        }
        return Result.OK();
    }

    //模糊查询
    @PostMapping("findTypeList")
    public Result findTypeList(@RequestBody TypeVO typeVO) {
        List<TypeVO> typeList = typeTemplateService.findTypeList(typeVO);
        return Result.OK().setData(typeList);
    }

    //增加
    @PostMapping("InsertType")
    public Result InsertType(@RequestBody TypeVO typeVO){
        int i = typeTemplateService.InsertType(typeVO);
        if (i <= 0) {
            return Result.ERROR();
        }
        return Result.OK();
    }

    //删除
    @DeleteMapping("deleteType/{arr}")
    public Result deleteType(@PathVariable Long[] arr){
        int i = typeTemplateService.deleteType(arr);
        if (i <= 0) {
            return Result.ERROR();
        }
        return Result.OK();
    }
}
